﻿using DataAccess.DTO;
using DataAccess.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYD.DataAccess;
using XYD.Framework.Data;
using XYD.Framework.Data.Dapper;

namespace DataAccess.DAL
{
    public class AgencyRobotDAL : BaseDAL
    {
        public AgencyRobot Get(int id)
        {
            return CONN_TopicSharing.Get<AgencyRobot>(id);
        }

        public bool Insert(AgencyRobot user)
        {
            return CONN_TopicSharing.Insert<AgencyRobot>(user);
        }

        public bool Update(AgencyRobot user)
        {
            return CONN_TopicSharing.Update<AgencyRobot>(user);
        }

        public bool Delete(int id)
        {
            string sql = " update [dbo].[AgencyRobot] set isdel=1 where id=@id  ";

            return CONN_TopicSharing.Excute(sql, new { id = id }) > 0;

        }


        public List<AgencyRobot> GetAgencyRobots()
        {
            List<AgencyRobot> users = new List<AgencyRobot>();

            users = CONN_TopicSharing.SQLQuery<AgencyRobot>(" select * from AgencyRobot ", null).ToList();

            return users;
        }

        public List<AgencyRobot> ListPage(string strWhere, string orderbys, int pagesize, int pageindex, out int count,object param)
        {
            List<AgencyRobot> users = new List<AgencyRobot>();
            if (string.IsNullOrEmpty(strWhere))
            {
                strWhere = " 1=1 ";
            }

            if (string.IsNullOrEmpty(orderbys))
                orderbys = "id";

            users = CONN_TopicSharing.SQLPager<AgencyRobot>("*", " AgencyRobot", strWhere, "", "", orderbys, pagesize, pageindex, out count, param).ToList();

            return users;
        }

        //判断是否存在
        public bool IsExist(string AU_Name, string FlockName)
        {
            return CONN_TopicSharing.SQLQuery<int>(string.Format(" select count(1) from AgencyRobot where AU_Name='{0}' and Isdel=0  and FlockName='{1}' ", AU_Name, FlockName), null).FirstOrDefault() > 0;

        }

        /// <summary>
        /// 通过群名称获取代理信息
        /// </summary>
        /// <param name="promoter_id"></param>
        /// <param name="flock_type"></param>
        /// <param name="flockName"></param>
        /// <returns></returns>

        public List<Agency_Flock_Info> GetFlockInfos(int promoter_id, int flock_type, List<string> flockName)
        {

            string sqlWhere = " where IsDel=0  ";

            string flockNameWhere = "";

            if (promoter_id > 0)
            {
                sqlWhere += " and promoterid=@promoter_id ";
            }
            else
            {
                return null;
            }

            if (flock_type > 0)
            {
                sqlWhere += " and flocktype=@flock_type ";
            }
            if (flockName.Count > 0)
            {
                for (int i = 0; i < flockName.Count; i++)
                {
                    var item = flockName[i];
                    if (!string.IsNullOrEmpty(item))
                    {
                        flockNameWhere += "select '"+item+"' n ";

                        if (i != flockName.Count - 1)
                            flockNameWhere += " union all ";
                    }
                }
                if (!string.IsNullOrEmpty(flockNameWhere.Trim()))
                {
                    flockNameWhere = string.Format(" right join  ({0})  t on ar.flockname=t.n", flockNameWhere); ;
                }
            }
            #region sql 
            //select * from [dbo].[AgencyRobot] ar
            // right join 
            //(
            // select '123' n
            // union all 
            // select '111' n
            // union all 
            // select '222' n
            //)  t 
            //on   ar.flockname=t.n
            //where promoterid=1 and IsDel=0 and flocktype=1 
            #endregion
            string sql = string.Format(" select ar.au_id,au.site_pid,au.wechat_pid,au.qq_pid,ar.FlockType flock_type,ar.FlockSendSecond flock_send_second, ar.ImageTextSecond image_text_second ,ar.SendTimeStr send_time_str,ar.alimama_id  from  ( select * from [dbo].[AgencyRobot] ar  {0} {1}  ) ar  left join [dbo].[AgencyUsers] au  on ar.AU_Id=au.id ", flockNameWhere, sqlWhere);

            List<Agency_Flock_Info> users = new List<Agency_Flock_Info>();


            users = CONN_TopicSharing.SQLQuery<Agency_Flock_Info>(sql, new { promoter_id = promoter_id, flock_type = flock_type }).ToList();

            return users;

        }


        /// <summary>
        /// 获取列表通过代理id 
        /// </summary>
        /// <param name="au_id"></param>
        /// <returns></returns>
        public List<AgencyRobot> GetListByAu_id(int au_id)
        {
            List<AgencyRobot> users = new List<AgencyRobot>();
            if (au_id<=0)
            {
                return null;
            }
            string sql = "select * from [dbo].[AgencyRobot] where au_id=@au_id and isdel=0 and isStart=1 order by id";


            users = CONN_TopicSharing.SQLQuery<AgencyRobot>(sql, new { au_id = au_id }).ToList();

            return users;
        }



        public bool UpdateFlockInfo(int FlockSendSecond, string SendTimeStr,int au_id)
        {
            string sql = "update [AgencyRobot] set FlockSendSecond=@FlockSendSecond,SendTimeStr=@SendTimeStr where  au_id=@au_id";
            return CONN_TopicSharing.Excute(sql, new { FlockSendSecond = FlockSendSecond, SendTimeStr = SendTimeStr, au_id = au_id }) > 0;
        }


    }
}
